package com.myteam.fugaocrm.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.myteam.fugaocrm.common.ApplicationContextAware;
import com.myteam.fugaocrm.modle.FgProjectLog;
import com.myteam.fugaocrm.modle.FgTask;
import com.myteam.fugaocrm.sqldb.mapper.FgProjectLogMapper;
import com.myteam.fugaocrm.sqldb.mapper.FgRemindMessageMapper;
import com.myteam.fugaocrm.sqldb.mapper.FgTaskMapper;

/**
 * 售前日志提醒
 * @author BZX
 *
 */
public class RemindLog {

	private FgTaskMapper fgTaskMapper = (FgTaskMapper) ApplicationContextAware.getApplicationContext().getBean(FgTaskMapper.class);
	private FgProjectLogMapper fgProjectLogMapper = (FgProjectLogMapper) ApplicationContextAware.getApplicationContext().getBean(FgProjectLogMapper.class);
	private FgRemindMessageMapper fgRemindMessageMapper = (FgRemindMessageMapper) ApplicationContextAware.getApplicationContext().getBean(FgRemindMessageMapper.class);
	
	public void work() {
		try {
        	//批量查询任务
        	List<FgTask> taskList = fgTaskMapper.all();
          
        	//依次循环执行任务
        	for(FgTask task : taskList) {
        		//售前日志
        		if(task.getTaskId() == 1 && task.getIsDelete() == 0) {
        			
        			List<FgProjectLog> logList = fgProjectLogMapper.selectToTask();//新数据
        			
    				//修改LastTime
    				Date date = new Date();
    				fgTaskMapper.updateLogTime(date);
    				
    				//删除数据
    				fgRemindMessageMapper.deleteRemindByTypeId(1);
        			
        			//处理新增
        			for(FgProjectLog temp : logList) {
        				Map<String, Object> param = new HashMap<String,Object>();
        				param.put("typeId", 1);
        				param.put("logId", temp.getLogId());
        				param.put("projectId", temp.getProjectId());
        				param.put("remindContent", "【" + temp.getProjectName() + "】项目有售前日志");
        				param.put("modifiedDate", new Date());
        				
        				fgRemindMessageMapper.insertLog(param);
        			}
        		}
        	}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
}
